
*******CREATING POST BONUS SOCIAL MISSION DATA FOR TABLE 3***********



set more off
clear
clear matrix
capture log close


	
			
//--> TREATMENT PERIOD MPRS DATA 
	*********************************************************************************************************************************************************
		
		
		//--> FWCO PANEL: Maps each FAs CO portfolio
		
				use "$data/FWCO_panel.dta"
				
				
		//--> MERGE w/ CO Month Level MPRS data		
				
				sort co_code month 
				merge co_code month using "$data/MPRS_comthlevel.dta"
				keep if _m==3
				drop _m
				
				//--> KEEP AUDITED DATA ONLY
				
					keep if cro_att_ ==1
					
				g num_co = 1
				recode co_training_ (2 = 0)
				recode other_meet_ (1=1)(2=0)
					
		//--> CALCULATE PERFORMANCE AT FA MONTH LEVEL
			*****************************************
			
			local mprs_var num_co num_mem_ curr_num_mem_ num_memb_sav_ sav_collected_ co_training_ other_meet_
			collapse (sum) `mprs_var', by(fw_code month)
			
			
			//--> PERFORMANCE VARS
			
				g other_meet_per_co = other_meet_/num_co
				g training_per_co = co_training_/num_co
				g savings_per_co = sav_collected_/num_co
				g attendance_per_mem = num_mem_/curr_num_mem_    //--> BONUS TRIGGER
				g saver_per_mem = num_memb_sav_/curr_num_mem_    //--> BONUS TRIGGER
				
			
				//--> NEW COS FORMED AND DEAD COs
				
						preserve
						use "$data/MIS_comthlevel.dta", clear
						bys co_code: keep if _n==1
						keep co_code birth alive_co alive_dead_co credit_gp
						sort co_code
						tempfile temp_birth
						save `temp_birth'
						
						use "$data/FWCO_panel.dta", clear
						sort co_code
						merge co_code using `temp_birth'
						drop _m
						
						//--> IDENTIFYING NEW CO FORMATION MONTH
						
								g num_new_co = (birth==month)
								
						g weighted_num_new_co = coshare_weight*num_new_co       //-->BONUS TRIGGER
						g weighted_alive_dead_co = coshare_weight*alive_dead_co
						
							//--> 	KEEP ONLY COS (DROP CREDIT GROUP)
							
									drop if credit_gp==1
						
						collapse (sum) weighted_num_new_co weighted_alive_dead_co, by(fw_code month)
						sort fw_code month
						tempfile temp_newco
						save `temp_newco'
						restore
						
				sort fw_code month
				merge fw_code month using `temp_newco', nok
				drop _m
				
		//--> CALCULATE AVERAGE PERFORMANCE (FA LEVEL)
		*****************************************
 		
				local social_var weighted_num_new_co saver_per_mem attendance_per_mem weighted_alive_dead_co savings_per_co other_meet_per_co training_per_co
				collapse (mean) `social_var', by(fw_code)
					
				//--> CALCULATE CO QUALITY INDEX
						*****************************************
					
						//--> STANDARDIZE THE SIX CREDIT OUTCOMES (BASED ON CONTROL GROUP)
						
									sort fw_code
									merge fw_code using "$data/FWHR.dta", nok
									drop _m
									
							foreach m in `social_var' {
								sum `m' if treat==0, d
								local mean = r(mean)
								local sd = r(sd)
							
								gen std`m' = (`m' - `mean')/`sd'
								}
							
						//--> INVERSE THE SCALE OF NUMBER OF DEAD COs

							foreach x in weighted_alive_dead_co {
								replace std`x' = -1*std`x'
								}
								
								egen social_index = rowmean(stdweighted_num_new_co stdsaver_per_mem stdattendance_per_mem stdweighted_alive_dead_co stdsavings_per_co stdother_meet_per_co stdtraining_per_co)
								sum social_index if treat==0
								local mean = r(mean)
								local sd = r(sd)
								gen stdsocial_index = (social_index - `mean')/`sd'
				
				
		keep fw_code `social_var' stdsocial_index
		sort fw_code
		save "$data/social_fa_vars.dta", replace

					
				
				
